// example/order_detail/order_detail.js
var app = getApp()
import * as Api from '../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasGoneAway: false,
    orderId: '',
    orderinfo: []
  },
  copyOrderId() {
    wx.setClipboardData({
      data: this.data.orderinfo.orderId,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功',
              icon: 'success',
              duration: 2000
            })
          }
        })
      }
    })
  },
  callSile() {
    wx.makePhoneCall({
      phoneNumber: app.globalData.merchant.contactMobile //仅为示例，并非真实的电话号码
    })
  },
  //确认收货
  recvGoods(e) {
    let optmessage = '确认收货?'
    if (this.data.orderinfo.refundStatus === 'APPLY_REFUND' || this.data.orderinfo.refundStatus === 'WAIT_REFUND') {
      optmessage = '订单正在退款，是否' + optmessage
    }


    let self = this
    wx.showModal({
      title: '提示',
      content: optmessage,
      success: function (res) {
        if (res.confirm) {
          let url = app.globalData.baseUrl1 + '/order/recvgoods'
          let data = {
            token: app.globalData.token,
            userId: app.globalData.userId,
            orderId: self.data.orderinfo.orderId
          }


          let callBack = (res) => {
            if (res == null) {

              console.log("网络请求错误3！");
              return;
            }

            if (res.data.status != 200) {
              console.log("请求数据错误3");
              return
            }
            if (res.data.status == 200) {
              console.log('确认收货', res)
              self.getOrderInfo(self.data.orderinfo.orderId)
            }
          }
          Api.reqFormPost(url, data, callBack)
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })


  },
  goShip(e) {
    if (!this.data.hasGoneAway) {
      this.setData({ hasGoneAway: true })
      wx.navigateTo({
        url: '/pages/core/ship/ship?orderid=' + this.data.orderinfo.orderId,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },

  //支付订单
  payOrder(e) {
    let self = this
    let url = app.globalData.baseUrl1 + '/order/paybyorder'
    let data = {
      token: app.globalData.token,
      userId: app.globalData.userId,
      orderId: self.data.orderinfo.orderId
    }


    let callBack = (res) => {
      console.log(1, res)
      if (res == null) {

        console.log("网络请求错误3！");
        return;
      }

      if (res.data.status != 200) {
        console.log("请求数据错误3");
        return
      }
      if (res.data.status == 200) {



        let url = app.globalData.baseUrl1 + '/wechat/pay/miniapp'
        let data = {
          token: app.globalData.token,
          userId: app.globalData.userId,
          openId: app.globalData.openid,
          body: app.globalData.merchant.merchantName,
          outTradeNo: res.data.data.payId,
          totalFee: res.data.data.totalPrice
        }


        let callBack = (res2) => {
          console.log(2, res2)
          if (res2 == null) {

            console.log("网络请求错误3！");
            return;
          }

          if (res2.data.status != 200) {
            console.log("请求数据错误3");
            return
          }
          if (res2.data.status == 200) {


            wx.requestPayment({
              'appId': app.globalData.appid,
              'timeStamp': res2.data.data.timeStamp,
              'nonceStr': res2.data.data.nonceStr,
              'package': res2.data.data.package,
              'signType': 'MD5',
              'paySign': res2.data.data.paySign,
              'success': function (response) {

                self.getOrderInfo(self.data.orderinfo.orderId)

                wx.showToast({
                  title: '支付成功',
                  icon: 'success',
                  duration: 2000
                })
                console.log(response)
              },
              'fail': function (res) {
                console.log(res)
              },
              'complete': function (res) {
                console.log(res)
              }
            })
          }

        }
        Api.reqFormPost(url, data, callBack)

      }

    }
    Api.reqFormPost(url, data, callBack)


  },
  delOrder(e) {

    let self = this
    wx.showModal({
      title: '提示',
      content: '确定关闭该订单？',
      success: function (res) {
        if (res.confirm) {
          let url = app.globalData.baseUrl1 + '/order/cancel';
          let data = {
            token: app.globalData.token,
            userId: app.globalData.userId,
            orderId: self.data.orderinfo.orderId
          }
          let callBack = (res) => {

            if (res == null) {
              console.log("网络请求错误！");
              return;
            }

            if (res.data.status != 200) {
              console.log("请求数据错误");
              return;
            }
            if (res.data.status === 200) {
              self.getOrderInfo(self.data.orderinfo.orderId)
              wx.showToast({
                title: '取消成功',
                icon: 'success',
                duration: 2000
              })
            }
          }
          Api.reqFormPost(url, data, callBack)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  refund() {


    if (!this.data.hasGoneAway) {
      this.setData({ hasGoneAway: true })
      wx.navigateTo({
        url: '/pages/core/refund/refund?orderid=' + this.data.orderinfo.orderId + '&paytotal=' + this.data.orderinfo.payTotal,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },
  succedRefund() {

    let self = this

    wx.showModal({
      title: '提示',
      content: '确定退款？',
      success: function (res) {
        if (res.confirm) {
          let url = app.globalData.baseUrl1 + '/order/refund/succeed'
          let data = {
            token: app.globalData.token,
            userId: app.globalData.userId,
            orderId: self.data.orderinfo.orderId
          }
          let callBack = (res) => {
            console.log('确认退款', res)

            if (res == null) {
              console.log("网络请求错误1！");
              return;
            }

            if (res.data.status != 200) {
              console.log(res.data)
              console.log("网络请求错误2！");

              return;
            }

            if (res.data.status == 200) {
              wx.showToast({
                title: '发送成功',
                icon: 'success',
                duration: 2000
              })
              self.getOrderInfo(self.data.orderinfo.orderId)

            }
          }
          Api.reqFormPost(url, data, callBack)



        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.merchant === null) {
      app.doLogin(this.onLoad, this.onShow, options);
      return
    }
      this.setData({
        orderId: options.orderid
      })

  },

  getOrderInfo(orderid) {
    let self = this
    wx.request({
      method: 'post',
      url: app.globalData.baseUrl1 + '/order/queryorderdetail',
      data: {
        token: app.globalData.token,
        userId: app.globalData.userId,
        orderId: orderid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        console.log('订单详情', res.data)
        self.setData({
          orderinfo: res.data.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.merchant === null) {
      return
    } 
    if (app.globalData.merchant.mustLogin || app.globalData.userId === null) {
      if (app.globalData.userId === null) {
        wx.navigateTo({
          url: '/pages/core/login/login',
          success() {
            wx.showToast({
              title: '请先登陆',
              icon: 'loading',
              duration: 1000
            })
          }
        })
        return
      }
    }
    this.setData({ hasGoneAway: false })
    this.getOrderInfo(this.data.orderId)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})